<template>
  <div>
    <h1>测试 axios</h1>
  </div>
</template>

<script>
import { getTestJson } from "@/api/json/test";
import getGlobalParams from "@/utils/getGlobalParams";

export default {
  name: "axios",
  data() {
    return {
      userLevel: [],
    };
  },
  created() {
    this.axiosTest();
    // this.getJson();
  },
  mounted() {
    console.log("aa", getGlobalParams.get("userLevel"));
  },
  computed: {},
  watch: {},
  methods: {
    getJson() {
      let request = new XMLHttpRequest();
      request.open("get", "./config/globalParams.json", true);
      request.send();
      // request.onreadystatechange = function() {
      //   if(request.status === 200) {
      //     let json = JSON.parse(request.responseText)
      //     console.log("json", json)
      //   }
      // }
      request.onreadystatechange = function () {
        if (request.readyState === 4) {
          if (request.status === 200) {
            let json = JSON.parse(request.responseText);
            console.log("json", json["userLevel"])
            const e = new CustomEvent("ready");
            document.dispatchEvent(e);
          } else {
            console.error(`未找到全局参数配置的文件`);
          }
        }
      };
    },
    axiosTest() {
      this.$axios.get("/json/test.json")
      // .then(res =>{
      //   console.log("getget", res);
      // })
      this.$axios({
        method: "get",
        url: "/json/test.json",
      })
        .then((res) => {
          let content = res;
          console.log("content", content);
          // 成功回调，res为响应体
        })
        .catch((e) => {
          // 失败回调,e为错误提示
          this.$message({
            type: "error", // success/warning/info/error
            message: "请求出错：" + e,
          });
        });
      this.$axios.get("/json/test.json").then((res) => {});
      getTestJson("test").then((res) => {
        console.log("res", res);
      });
    },
  },
};
</script>
